// pages/task/taskDetail.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
const systemInfo = wx.getWindowInfo();
const bottomInset = Math.abs(systemInfo.safeArea.bottom - systemInfo.windowHeight);      

//按钮颜色
var button_color = "#FF4B55";
//按钮是否可以点击
var button_enable = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    task_no: '',
    task_title: '任务标题',
    task_time: '0小时',
    task_surplus: 0,
    task_end_time: '0天结束',
    task_price: '+0元/次',
    user_header: '/images/person/defaultHeader.png',
    user_name: '',
    icon_view_bid: 1,
    task_description: '',
    steps: [],
    task_requirement: '',
    requirement_images: [],
    button_status: '领取任务',
    taskStatus: '待领取',
    publisherPlatformId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      scrollViewBottomInset: bottomInset+64,
      bottomInset: bottomInset,
      task_no: options.taskNo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.requestTaskDetail();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //头像加载错误处理
  headerImageLoadFail() {
    this.setData({
      user_header: "/images/person/defaultHeader.png"
    })
  },
  //请求任务详情
  requestTaskDetail() {
    let obj = {
      method: 'GET',
      url: '/task/' + this.data.task_no,
      showLoading: true,
      data: {
        
      }
    }
    httpUtils.request(obj).then(res => {
      //判断状态
      if (res.data.taskStatus == "待领取") {
        button_color = "#FF4B55";
        button_enable = false;
      } else if (res.data.taskStatus == "待提交") {
        button_color = "#4BBE97";
        button_enable = false;
      } else if (res.data.taskStatus == "已拒绝") {
        button_color = "#A2A2A2";
        button_enable = false;
      } else {
        button_color = "#A2A2A2";
        button_enable = true;
      }
      this.setData({
        task_title: res.data.taskTitle,
        task_time: res.data.bidSubmitDurationTimeDisplay,
        task_surplus: res.data.leftBidTime,
        task_end_time: res.data.endTimeDisplay,
        task_price: res.data.rewardAmountDisplay,
        user_header: res.data.publisherVartar,
        user_name: res.data.publisherName,
        icon_view_bid: res.data.bidTimesByUser,
        task_description: res.data.taskDescription, 
        steps: res.data.taskStepList,
        task_requirement: res.data.taskRequirement,
        requirement_images: res.data.taskRequirementImages,
        button_status: res.data.statusLabel, 
        taskStatus: res.data.taskStatus,
        button_color: button_color,
        button_enable: button_enable,
        publisherPlatformId: res.data.publisherPlatformId
      })
    }).catch(err => {
      console.log(err)
    });
  },
  //点击复制内容
  actionCopyInfo(event) {
    console.log(event.currentTarget.dataset.info)
    wx.setClipboardData({
      data: event.currentTarget.dataset.info,
      success: function(res) {
        ui.showToast("复制成功");
      }
    });
  },
  //点击复制链接
  actionCopyLink(event) {
    console.log(event.currentTarget.dataset.info)
    wx.setClipboardData({
      data: event.currentTarget.dataset.info,
      success: function(res) {
        ui.showToast("复制成功");
      }
    });
  },
  //点击图片预览
  previewImage(event){
    var currentUrl = event.currentTarget.dataset.src //获取当前点击图片链接
    var urls = event.currentTarget.dataset.urls //获取当前图片列表
    wx.previewImage({
      current:currentUrl,//当前点击的预览图片地址
      urls: urls,//需要预览的图片列表，这是必须设置的属性
    })
  },
  //点击任务按钮
  actionTask() {
    if (this.data.taskStatus == "待领取") {
      this.requestSubmitTask();
    } else if (this.data.taskStatus == "待提交") {
      wx.navigateTo({
        url: '/pages/task/taskSubmit?taskNo='+this.data.task_no,
      })
    }
  },
  //领取任务接口
  requestSubmitTask() {
    var that = this;
    let obj = {
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      url: '/task-order/' + wx.getStorageSync('USERDATA').platformId + '/' + this.data.task_no + '/bid',
      showLoading: true,
      data: {
        platformId: wx.getStorageSync('USERDATA').platformId,
        taskNo: this.data.task_no
      }
    }
    httpUtils.request(obj).then(res => {
      ui.showToast("领取成功");
      that.requestTaskDetail();
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //点击进入发布者个人页面
  actionFriend() {
    wx.navigateTo({
      url: '/pages/friend/friend?userId='+this.data.publisherPlatformId,
    })
  }
})